/*******************************************************************************
 **  Name: irc.cpp                                         **
 **  Description:                                                             **
 **                                                                           **
 **  Open Source Initiative (OSI) Approved License                            **
 **                                                                           **
 **  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
 **  All rights reserved.                                                     **
 **                                                                           **
 **  The contents of this file are subject to the terms of the CDDL:          **
 **  Common Development and Distribution License (the "License").             **
 **  You may not use this file except in compliance with the License.         **
 **                                                                           **
 **  You can obtain a copy of the license at $PROJECT_ROOT/LICENSE            **
 **  or http://www.opensolaris.org/os/licensing.  This code is Open Source    **
 **  and you are free to use it within the confines of the license, even      **
 **  for your own closed-source commercial projects, as long as you follow    **
 **  the terms set forth in the CDDL.                                         **
 **                                                                           **
 **  When distributing Covered Code, include this CDDL HEADER in each         **
 **  file and include the License file at $PROJECT_ROOT/LICENSE.              **
 **  If applicable, add the following below this CDDL HEADER, with the        **
 **  fields enclosed by brackets "[]" replaced with your own identifying      **
 **  information: Portions Copyright [yyyy] [name of copyright owner]         **
 **                                                                           **
 ******************************************************************************/

#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <strings.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>

#include "irc.h"

// Created on November 24, 2010, 12:37 AM
// Modified $Date$ by $Author$
#define irc_cpp_ID   "$Id$"

static int readline(register int, register char*, register int);
static int writen(register int fd, register char * ptr, register int nbytes);

ircmsg *
IRC::Parse()
{
    char    rcvbuf[IRC_MAXLINE + 1];
    int n = readline(sockfd,rcvbuf,IRC_MAXLINE);


    ircmsg * msg = new ircmsg();


    return msg;
}


void
IRC::SendNick()
{
    char    rcvbuf[IRC_MAXLINE + 1];
    strcpy(rcvbuf,IRC_CMD_NICK);
    writen(sockfd,rcvbuf,strlen(rcvbuf));
}

void
IRC::SendUser()
{
    char    rcvbuf[IRC_MAXLINE + 1];
    strcpy(rcvbuf,IRC_CMD_USER);
    writen(sockfd,rcvbuf,strlen(rcvbuf));
}

int
IRC::Connect(const char* srv, int port)
{
    sockaddr_in     SrvAddr;
    char            rcvbuf[IRC_MAXLINE + 1];
    hostent *       hostptr;
    in_addr *       inetaddr;

    if ((hostptr = gethostbyname(DEFAULT_SERVER)) == NULL)
        return -1;
    if ((inetaddr = (in_addr*)*hostptr->h_addr_list) == NULL)
        return -2;

    bzero((char*)&SrvAddr,sizeof(SrvAddr));
    SrvAddr.sin_family      = AF_INET;
    SrvAddr.sin_addr.s_addr = inet_addr(inet_ntoa(*inetaddr));
    SrvAddr.sin_port        = htons(DEFAULT_PORT);

    if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
        return -3;
    if (connect(sockfd,(sockaddr*)&SrvAddr,sizeof(SrvAddr)) < 0)
        return -4;

    return 1;
}


int readline(register int fd, register char * ptr, register int maxlen)
{
    int n, rc;
    char c;

    for (n = 1; n < maxlen; n++)
    {
        if ((rc = read(fd,&c,1)) == 1)
        {
            *ptr++ = c;
            if (c == '\n')
                break;
        } else if (rc == 0)
        {
            if (n == 1)
                return 0;
            else
                break;
        } else
            return (-1);
    }
    *ptr = 0;
    return (n);

}

int writen(register int fd, register char * ptr, register int nbytes)
{
    int nleft, nwritten;

    nleft = nbytes;
    while (nleft > 0)
    {
        nwritten = write(fd,ptr,nleft);
        if (nwritten <= 0)
            return(nwritten);
        nleft -= nwritten;
        ptr += nwritten;
    }
    return (nbytes - nleft);
}
                                                                         


ircmsg::ircmsg()
{
    cmd = to = from = data = NULL;
}

ircmsg::~ircmsg()
{
    if (cmd) delete [] cmd;
    if (to) delete [] to;
    if (from) delete [] from;
    if (data) delete [] data;
}